package jmine.tec.hydra.receive.impl.file;

import java.io.File;
import java.io.FileFilter;
import java.io.Serializable;
import java.util.regex.Pattern;

import jmine.tec.annotations.Unmodifiable;

/**
 * {@link AbstractHydraFileFilter} that matches against a regular expression. The regular expression MUST match the ENTIRE string
 * 
 * @author takeshi
 */
@Unmodifiable
public class RegularExpressionFileFilter extends AbstractHydraFileFilter implements FileFilter, Serializable {

    private final Pattern fileNamePattern;

    /**
     * The most basic constructor that receives the {@link Pattern}
     * 
     * @param fileNamePattern {@link Pattern}
     */
    public RegularExpressionFileFilter(Pattern fileNamePattern) {
        this.fileNamePattern = fileNamePattern;
    }

    /**
     * C'tor
     * 
     * @param regex the String that is the regular expression
     * @param patternFlags the regular expression flags - like {@link Pattern#CASE_INSENSITIVE} etc
     */
    public RegularExpressionFileFilter(String regex, int patternFlags) {
        this(Pattern.compile(regex, patternFlags));
    }

    /**
     * C'tor
     * 
     * @param regex the String that is the regular expression
     * @param caseSensitive if true, the regex will be case SENSITIVE
     */
    public RegularExpressionFileFilter(String regex, boolean caseSensitive) {
        this(regex, caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
    }

    /**
     * C`tor that is case INSENSITIVE
     * 
     * @param regex the regular expression
     */
    public RegularExpressionFileFilter(String regex) {
        this(regex, false);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean doAcceptFile(File file) {
        return fileNamePattern.matcher(file.getName()).matches();
    }

}
